#!/bin/sh
# run in an eiffel.sh environment (Visual Eiffel)

main(){
 settings
 ensure_compiler || exit 1
 ensure_variables || exit 1
 compile "$@"
}

settings(){
 B=/tmp/$USER/
 P=$B/V_Eiffel/
 CACHE=$HOME/cache
}

ensure_compiler(){
 if [ ! -d "$P" ] ; then
  ARC="$CACHE/vsetup5.tgz"
  if [ ! -f "$ARC" ] ; then
   echo "courtesy to http://visual-eiffel.com/" >&2
   wget http://tools.otsys.de/vsetup5.tgz || exit 1
  fi
  tar -xvzf $ARC -C $B || exit 1
 fi
}

ensure_variables(){
 export VE_Bin=$P/bin
 export VE_Lib=$P/lib
 export VE_Data=$P/bin
 export VE_OS=Linux
 export PATH=$VE_Bin:$PATH
 export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$VE_Bin
}

compile(){
SRC=$1
ROOTCLASS=`
 grep '^class' "$SRC" |
 head -n 1 |
 sed 's/^class *//g ; s/^\([A-Z0-9_]*\).*$/\1/'`
cat << EOF > system.xace
<?xml version="1.0"?>

<system name="my_system_name">
        <root class="$ROOTCLASS" creation="main"/>
        <option name="console_application" value="true"/>
        <option name="debugger" value="false"/>
        <option name="dynamic_runtime" value="true"/>
        <option name="linker" value="default"/>
        <option name="target" value="exe"/>

        <cluster name="my_cluster_name" location="."/>
        <mount location="${VE_Lib}/kernel/library.xace"/>
        <mount location="${VE_Lib}/misc/pool/library.xace"/>
</system>
EOF
vec -a:system.xace &&
./my_system_name &&
echo
}

main "$@"
